using Commons.Models.OperationsContext;
using Commons.Models.Settings.Matchers.Operators;
using Commons.Models.Stats;
using Commons.Services.Stats;

namespace Commons.Models.Locks
{
	public class UserStatsUnlockConditionChecker : IUnlockConditionChecker
	{
		public UserStatsService UserStatsService { protected get; set; }

		public bool CheckCondition(IUnlockCondition condition, IOperationContext context)
		{
			UserStatsUnlockCondition userStatsCondition = condition as UserStatsUnlockCondition;

			UserStats stats = UserStatsService.GetStats();

			IRelationalOperator<int> relationalOperator = userStatsCondition.RelationalOperator;

			foreach (IStatUnit unit in userStatsCondition.GetRequiredValuesUnits())
			{
				if (!relationalOperator.Operate(stats.GetStat<IStat>(unit).Value, userStatsCondition.GetRequiredValue(unit)))
				{
					return false;
				}
			}

			foreach (IStatUnit unit in userStatsCondition.GetRequiredLevelsUnits())
			{
				if (!relationalOperator.Operate(stats.GetStat<LeveledStat>(unit).GetLevel().Num, userStatsCondition.GetRequiredLevel(unit)))
				{
					return false;
				}
			}

			return true;
		}
	}
}

